package com.iman.locker;

import android.app.Application;

import com.iman.dao.User;
import com.iman.locker.persistence.DbHelper;
import com.iman.locker.service.WatchDogService;
import com.tencent.bugly.crashreport.CrashReport;

/**
 * 应用
 * Created by esirong on 2015-12-04.
 */
public class MyApp extends Application {
    private static MyApp instance;
    private User user;//当前用户基本信息

    public static MyApp getInstance() {
        if (instance == null) {
            instance = new MyApp();
        }
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        Foreground.init(this);
        WatchDogService.start(this);
        CrashReport.initCrashReport(this, "900013773", false);
    }

    /**
     * 获取当前用户
     *
     * @return 当前用户信息
     */
    public User getUser() {
        if (user == null) {
            user = DbHelper.getInstance().getUser(1l);
        }
        if (user == null) {
            user = new User();
        }
        return user;
    }

    /**
     * 保存当前登入用户
     *
     * @param user 用户登录信息
     * @return 当前用户信息
     */
    public User setUser(User user) {
        this.user = user;
        DbHelper.getInstance().saveUser(user);
        return user;
    }

    /**
     * 是不是已初始化密码
     *
     * @return 如果已经设置过密码则返回true, 如果没有设置过任何密码则返回false
     */
    public boolean isPasswordHasBeenSet() {
        if (getUser() == null) {
            return false;
        } else {
            return true;
        }

    }
}
